home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch10 / Flake2.frm < prev    next >
Text File  |  1999-06-08  |  6KB  |  240 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  3. Begin VB.Form frmFlake2 
  4.    Caption         =   "Flake2"
  5.    ClientHeight    =   4785
  6.    ClientLeft      =   2280
  7.    ClientTop       =   1185
  8.    ClientWidth     =   5820
  9.    LinkTopic       =   "Form1"
  10.    PaletteMode     =   1  'UseZOrder
  11.    ScaleHeight     =   4785
  12.    ScaleWidth      =   5820
  13.    Begin MSComDlg.CommonDialog dlgFile 
  14.       Left            =   480
  15.       Top             =   1920
  16.       _ExtentX        =   847
  17.       _ExtentY        =   847
  18.       _Version        =   393216
  19.       CancelError     =   -1  'True
  20.    End
  21.    Begin VB.TextBox txtDepth 
  22.       Height          =   285
  23.       Left            =   600
  24.       MaxLength       =   3
  25.       TabIndex        =   0
  26.       Text            =   "3"
  27.       Top             =   0
  28.       Width           =   375
  29.    End
  30.    Begin VB.PictureBox picCanvas 
  31.       AutoRedraw      =   -1  'True
  32.       Height          =   4335
  33.       Left            =   1080
  34.       ScaleHeight     =   285
  35.       ScaleMode       =   3  'Pixel
  36.       ScaleWidth      =   261
  37.       TabIndex        =   3
  38.       Top             =   0
  39.       Width           =   3975
  40.    End
  41.    Begin VB.CommandButton cmdGo 
  42.       Caption         =   "Go"
  43.       Default         =   -1  'True
  44.       Enabled         =   0   'False
  45.       Height          =   375
  46.       Left            =   240
  47.       TabIndex        =   1
  48.       Top             =   480
  49.       Width           =   615
  50.    End
  51.    Begin VB.Label Label1 
  52.       Caption         =   "Depth"
  53.       Height          =   255
  54.       Index           =   0
  55.       Left            =   0
  56.       TabIndex        =   2
  57.       Top             =   0
  58.       Width           =   495
  59.    End
  60.    Begin VB.Menu mnuFile 
  61.       Caption         =   "&File"
  62.       Begin VB.Menu mnuFileOpen 
  63.          Caption         =   "&Open File..."
  64.          Shortcut        =   ^O
  65.       End
  66.    End
  67. End
  68. Attribute VB_Name = "frmFlake2"
  69. Attribute VB_GlobalNameSpace = False
  70. Attribute VB_Creatable = False
  71. Attribute VB_PredeclaredId = True
  72. Attribute VB_Exposed = False
  73. Option Explicit
  74.  
  75. Private Const PI = 3.14159
  76.  
  77. ' Coordinates of the points in the initiator.
  78. Private NumInitiatorPoints As Integer
  79. Private InitiatorX() As Single
  80. Private InitiatorY() As Single
  81.  
  82. ' Angles and distances for the generator.
  83. Private NumGeneratorAngles As Integer
  84. Private ScaleFactor As Single
  85. Private GeneratorDTheta() As Single
  86. ' Draw the complete snowflake.
  87. Private Sub DrawFlake(ByVal depth As Integer, ByVal length As Single)
  88. Dim i As Integer
  89. Dim x1 As Single
  90. Dim y1 As Single
  91. Dim x2 As Single
  92. Dim y2 As Single
  93. Dim dx As Single
  94. Dim dy As Single
  95. Dim theta As Single
  96.  
  97.     picCanvas.Cls
  98.  
  99.     ' Draw the snowflake.
  100.     For i = 1 To NumInitiatorPoints
  101.         x1 = InitiatorX(i - 1)
  102.         y1 = InitiatorY(i - 1)
  103.         x2 = InitiatorX(i)
  104.         y2 = InitiatorY(i)
  105.         dx = x2 - x1
  106.         dy = y2 - y1
  107.         theta = ATan2(dy, dx)
  108.         DrawFlakeEdge depth, x1, y1, _
  109.             theta, length
  110.     Next i
  111. End Sub
  112.  
  113. ' Recursively draw a snowflake edge starting at
  114. ' (x1, y1) in direction theta and distance dist.
  115. ' Leave the coordinates of the endpoint in
  116. ' (x1, y1).
  117. Private Sub DrawFlakeEdge(ByVal depth As Integer, ByRef x1 As Single, ByRef y1 As Single, ByVal theta As Single, ByVal dist As Single)
  118. Dim status As Integer
  119. Dim i As Integer
  120. Dim x2 As Single
  121. Dim y2 As Single
  122.  
  123.     If depth <= 0 Then
  124.         x2 = x1 + dist * Cos(theta)
  125.         y2 = y1 + dist * Sin(theta)
  126.         picCanvas.Line (x1, y1)-(x2, y2)
  127.         x1 = x2
  128.         y1 = y2
  129.         Exit Sub
  130.     End If
  131.  
  132.     ' Recursively draw the edge.
  133.     dist = dist * ScaleFactor
  134.     For i = 1 To NumGeneratorAngles
  135.         theta = theta + GeneratorDTheta(i)
  136.         DrawFlakeEdge depth - 1, x1, y1, theta, dist
  137.     Next i
  138. End Sub
  139. Private Sub CmdGo_Click()
  140. Dim depth As Integer
  141. Dim dx As Single
  142. Dim dy As Single
  143. Dim length As Single
  144.  
  145.     picCanvas.Cls
  146.     MousePointer = vbHourglass
  147.     DoEvents
  148.  
  149.     ' Get the parameters.
  150.     If Not IsNumeric(txtDepth.Text) Then txtDepth.Text = "5"
  151.     depth = CInt(txtDepth.Text)
  152.  
  153.     ' Find the distance between initiator points.
  154.     dx = InitiatorX(2) - InitiatorX(1)
  155.     dy = InitiatorY(2) - InitiatorY(1)
  156.     length = Sqr(dx * dx + dy * dy)
  157.  
  158.     ' Draw the snowflake.
  159.     DrawFlake depth, length
  160.  
  161.     MousePointer = vbDefault
  162. End Sub
  163. Private Sub Form_Load()
  164.     dlgFile.Filter = "Snowflake Files (*.sno)|*.sno"
  165.     dlgFile.InitDir = App.Path
  166. End Sub
  167.  
  168. Private Sub Form_Resize()
  169. Dim wid As Single
  170.  
  171.     ' Make the picCanvas as big as possible.
  172.     wid = ScaleWidth - picCanvas.Left
  173.     If wid < 120 Then wid = 120
  174.     picCanvas.Move picCanvas.Left, 0, wid, ScaleHeight
  175. End Sub
  176.  
  177. ' Load a snowflake definition file with format:
  178. '
  179. '   # Initiator points.
  180. '   (x1, y1)
  181. '   (x2, y2)
  182. '       :
  183. '   scalefactor
  184. '   # Generator angles.
  185. '   theta1
  186. '   theta2
  187. '       :
  188. Private Sub mnuFileOpen_Click()
  189. Dim file_name As String
  190. Dim fnum As Integer
  191. Dim theta As Single
  192. Dim i As Integer
  193.  
  194.     ' Allow the user to pick a file.
  195.     On Error Resume Next
  196.     dlgFile.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  197.     dlgFile.ShowOpen
  198.     If Err.Number = cdlCancel Then
  199.         Exit Sub
  200.     ElseIf Err.Number <> 0 Then
  201.         Beep
  202.         MsgBox "Error selecting file.", , vbExclamation
  203.         Exit Sub
  204.     End If
  205.     On Error GoTo 0
  206.  
  207.     file_name = Trim$(dlgFile.FileName)
  208.     dlgFile.InitDir = Left$(file_name, Len(file_name) _
  209.         - Len(dlgFile.FileTitle) - 1)
  210.  
  211.     ' Open the file.
  212.     fnum = FreeFile
  213.     Open file_name For Input Access Read As #fnum
  214.  
  215.     ' Read the initiator.
  216.     Input #fnum, NumInitiatorPoints
  217.     ReDim InitiatorX(0 To NumInitiatorPoints)
  218.     ReDim InitiatorY(0 To NumInitiatorPoints)
  219.     For i = 1 To NumInitiatorPoints
  220.         Input #fnum, InitiatorX(i), InitiatorY(i)
  221.     Next i
  222.     InitiatorX(0) = InitiatorX(NumInitiatorPoints)
  223.     InitiatorY(0) = InitiatorY(NumInitiatorPoints)
  224.  
  225.     ' Read the generator information.
  226.     Input #fnum, ScaleFactor, NumGeneratorAngles
  227.     ReDim GeneratorDTheta(1 To NumGeneratorAngles)
  228.     For i = 1 To NumGeneratorAngles
  229.         Input #fnum, theta
  230.         GeneratorDTheta(i) = theta * PI / 180
  231.     Next i
  232.  
  233.     Close #fnum
  234.  
  235.     Caption = "Flake2 [" & dlgFile.FileTitle & "]"
  236.     cmdGo.Enabled = True
  237. End Sub
  238.  
  239.  
  240.